package com.bfly.mall.report.service.impl;

import com.bfly.core.enums.SysError;
import com.bfly.core.exception.ServiceResponseException;
import com.bfly.mall.report.dao.ReportMapper;
import com.bfly.mall.report.dto.DataValueDTO;
import com.bfly.mall.report.service.IReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 长沙商脉科技有限公司
 * @date 2020/12/9 12:28
 */
@Service
public class ReportServiceImpl implements IReportService {
    @Autowired
    private ReportMapper reportMapper;

    /**
     * 校验查询时间
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/12/9 12:46
     */
    private void checkDate(Date start, Date end) {
        if (end.before(start)) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "结束时间需大于开始事件");
        }
        long days = (end.getTime() - start.getTime()) / (24 * 60 * 60 * 1000);
        if (days > 90) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "最多查询90天的记录");
        }
    }

    @Override
    public List<DataValueDTO> statisticsUserIncrease(Date start, Date end) {
        checkDate(start, end);
        List<DataValueDTO> list = reportMapper.getUserIncreaseReport(start, end);
        return list;
    }

    @Override
    public List<DataValueDTO> statisticsOrderIncrease(Date start, Date end) {
        checkDate(start, end);
        List<DataValueDTO> list = reportMapper.getOrderIncreaseReport(start, end);
        return list;
    }
}
